package com.eclab.hello.controller;

import com.eclab.hello.exception.UserException;
import com.eclab.hello.model.BO.RestResponseBo;
import com.eclab.hello.model.User;
import com.eclab.hello.properties.eclab.EclabProperties;
import com.eclab.hello.properties.eclab.EclabProperties2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.UUID;

/**
 * 描述:
 * Hello控制器
 */
@Api(value = "/", tags = "Hello控制器")
@RestController
@Slf4j
public class HelloController {

    @Autowired
    private EclabProperties2 eclabProperties;


    @GetMapping("/hello")
    public String sayHello(){
        return "Hello world!";
    }

    @GetMapping("/value")
    public String getValue(){
        return eclabProperties.getName()+"\n"+eclabProperties.getVersion();
    }
    @ExceptionHandler
    @PostMapping("/upload")
    public RestResponseBo uploadFile(@ApiParam(value = "editormd-image-file" , required = true) @RequestParam("editormd-image-file") MultipartFile multipartFile,
                                     HttpServletRequest request){
        if (multipartFile.isEmpty() || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            throw new UserException("文件为空");
        }
        String contentType = multipartFile.getContentType();
        log.info("contentType:{}", contentType);
        if (!contentType.contains("")) {
            throw new UserException("文件格式错误");
        }
        String root_fileName = multipartFile.getOriginalFilename();
        log.info("上传文件:name={},type={}", root_fileName, contentType);
        String filePath = "D:\\hello.img";
        log.info("图片保存路径={}", filePath);
        try {
            //保存图片到磁盘
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            FileInputStream fileInputStream = (FileInputStream) multipartFile.getInputStream();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath + File.separator + root_fileName));
            byte[] bs = new byte[1024];
            int len;
            while ((len = fileInputStream.read(bs)) != -1) {
                bos.write(bs, 0, len);
            }
            bos.flush();
            bos.close();
            if (StringUtils.isNotBlank(root_fileName)) {
                return RestResponseBo.ok(filePath+ File.separator+root_fileName, 200);
            }
            log.info("返回值：{}", RestResponseBo.ok());
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            throw new UserException("保存出错");
        }

    }



}
